/**
 * 
 */
package panel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.Border;

import modele.ModeleTablePoste;

import org.jdesktop.swingx.JXPanel;

import classe.*;

/**
 * @author guispeed4262
 *
 */
public class PanelDevisHaut extends JXPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JXPanel panDevisHaut;
	private Programme ent;
	private JCheckBox checkValid;
	private int nbr;
	private PageGestPoissonCrevette pageGestDevis;
	private JButton boutonPrint,boutonDel,boutonMod,boutonHist;
	private String ref;
	
	public PanelDevisHaut(Programme ent,int nbr,PageGestPoissonCrevette pageGestDevis,String ref){
		this.ent=ent;
		this.nbr=nbr;
		this.ref=ref;
		this.pageGestDevis=pageGestDevis;
		this.setMaximumSize(new Dimension(1000,200));
		this.setLayout(new BorderLayout());
		this.add(getPanDevisHaut(), BorderLayout.CENTER);
	}
	
	public JXPanel getPanDevisHaut(){
		if (panDevisHaut == null){
			panDevisHaut = new JXPanel();		
			panDevisHaut.setLayout(new FlowLayout());
			panDevisHaut.setPreferredSize(new Dimension(300,55));
			panDevisHaut.setBorder(BorderFactory.createLineBorder(Color.black));
			checkValid = new JCheckBox(new ImageIcon("./img/Thumbs up.gif"));
			checkValid.setSelected(ent.getDevis(nbr).isValider());
			if(ent.getDevis(nbr).isValider()){
				checkValid.setIcon(new ImageIcon("./img/Thumbs down.gif"));
				checkValid.setToolTipText("cliquer pour refusser");
			}else{				
				checkValid.setIcon(new ImageIcon("./img/Thumbs up.gif"));
				checkValid.setToolTipText("Cliquer pour valider");
			}
			checkValid.addActionListener(new ActionListener(){
        		public void actionPerformed (ActionEvent e ){
        			ent.getDevis(nbr).ChangeValider();
        			if(ent.getDevis(nbr).isValider()){
        			ent.ajoutFacture(new Facture(ent.getDevis(nbr).getIdClient(), ent.getDevis(nbr).getListePoste(),
        					Facture.getNouvRef(),new Date(),ent.getDevis(nbr).getReference(),ent.getDevis(nbr).getNumListe()));
        			}
        			pageGestDevis.miseAJour(null);
        			
        			
        		}
			});
			if(ent.getDevis(nbr).isHist()){
				checkValid.setEnabled(false);
			}
			
			
			panDevisHaut.add(checkValid);
			panDevisHaut.add(getBoutonMod());
			panDevisHaut.add(getBoutonDel());
			panDevisHaut.add(getBoutonPrint());
			panDevisHaut.add(getBoutonHist());
		}
		return panDevisHaut;
	}
	 public JButton getBoutonPrint(){
		 if(boutonPrint == null){
			 boutonPrint= new JButton(new ImageIcon("./img/Print.gif"));
			 boutonPrint.setToolTipText("Imprimer");
			 if(ent.getDevis(nbr).isHist()||ent.getDevis(nbr).isValider()){
				 boutonPrint.setEnabled(false);
				}
		 }
	    return boutonPrint;
	}
	 
	public JButton getBoutonDel(){
		if(boutonDel== null){
			boutonDel= new JButton(new ImageIcon("./img/Erase.gif"));
			boutonDel.setToolTipText("Supprimer");
			boutonDel.addActionListener(new ActionListener(){
        		public void actionPerformed (ActionEvent e ){
        			ent.suppDevis(ref);
        			pageGestDevis.miseAJour(null);
        		}        		
        	});
			if(ent.getDevis(nbr).isHist()||ent.getDevis(nbr).isValider()){
				boutonDel.setEnabled(false);
			}
		}
		return boutonDel;
	}
	public JButton getBoutonMod(){
		if(boutonMod== null){
			boutonMod= new JButton(new ImageIcon("./img/Modify.gif"));
			boutonMod.setToolTipText("Modifier");
			if(ent.getDevis(nbr).isHist()||ent.getDevis(nbr).isValider()){
				boutonMod.setEnabled(false);
			}
		}
		return boutonMod;
	}
	public JButton getBoutonHist(){
		if(boutonHist== null){
			boutonHist= new JButton(new ImageIcon("./img/History.gif"));
			boutonHist.setToolTipText("Placer dans l'historique \n\n "+ent.getDevis(nbr).tempsRestant());
			boutonHist.addActionListener(new ActionListener(){
        		public void actionPerformed (ActionEvent e ){
        			ent.getDevis(nbr).ChangeHist();
        			pageGestDevis.miseAJour(null);
        		}
			});
			if(ent.getDevis(nbr).isHist()){
				boutonHist.setEnabled(false);
			}
		}
		return boutonHist;
	}

}
